package com.py.custom.tms.application.jvm.classload;

import java.lang.reflect.Method;

public class MyTest21 {

    public static void main(String[] args) throws Exception{
        MyTest16 loader1 = new MyTest16("loader1");
        MyTest16 loader2 = new MyTest16("loader2");
        loader1.setPath("/Users/pengyou03/Desktop/");
        loader2.setPath("/Users/pengyou03/Desktop/");

        //前提：把MyPerson.class复制到指定路径，删除idea下的MyPerson.class；
        //结论：类转换报错
        //Caused by: java.lang.ClassCastException: com.py.custom.tms.application.jvm.classload.MyPerson cannot be cast to com.py.custom.tms.application.jvm.classload.MyPerson
        //解释：由于MyPerson是由两个不同的类加载器(loader1、loader2)加载的，所以Class不相等；进行转换会报错。

        Class<?> clazz1 = loader1.loadClass("com.py.custom.tms.application.jvm.classload.MyPerson");
        Class<?> clazz2 = loader2.loadClass("com.py.custom.tms.application.jvm.classload.MyPerson");

        System.out.println(clazz1 == clazz2);

        Object object1 = clazz1.newInstance();
        Object object2 = clazz2.newInstance();

        Method method = clazz1.getMethod("setMyPerson", Object.class);
        method.invoke(object1, object2);
    }
}
